package com.liuhong.shoping.controller;


import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuhong.shoping.common.ResponseMessage;
import com.liuhong.shoping.dao.SystemInfoMapper;
import com.liuhong.shoping.entity.SystemInfo;
import com.liuhong.shoping.vo.view.SystemInfoPageVO;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhong
 * @since 2021-01-11
 */
@RestController
@RequestMapping("/systemInfo")
public class SystemInfoController {
	
	@Autowired
	private SystemInfoMapper systemInfoMapper;
	/**
	 * @Param{ SystemInfoPageVO }
	 * 查询系统信息
	 */
	@PostMapping("getSystemInfo")
    public ResponseMessage getSystemInfo(@RequestBody SystemInfoPageVO system) 
	{
		try {
			QueryWrapper<SystemInfo> queryWrapperSystemInfo = new QueryWrapper<>();
			queryWrapperSystemInfo.lambda()
				.eq(
						StringUtils.isNotBlank(system.getSystemName()),
						SystemInfo::getSystemName, 
						system.getSystemName()
					)
				.eq(
						StringUtils.isNotBlank(system.getAppId()),
						SystemInfo::getAppId, 
						system.getAppId()
					)
				.eq(
						StringUtils.isNotBlank(system.getSystemStatus()),
						SystemInfo::getSystemStatus, 
						system.getSystemStatus()
					)
				.eq(
						null != system.getCreatedTime(),
						SystemInfo::getCreatedTime, 
						system.getCreatedTime()
					)
				.eq(
						StringUtils.isNotBlank(system.getSystemStatus()),
						SystemInfo::getSystemAddress, 
						system.getSystemAddress()
					)
				.orderByDesc(SystemInfo::getCreatedTime);
	        IPage<SystemInfo>  sysIPage=  systemInfoMapper
	                .selectPage(
	                		new Page<>(system.getCurrentPage(),system.getPageSize()),
	                		queryWrapperSystemInfo);
	        List<SystemInfo> list = sysIPage.getRecords();
	        Integer selectCount = systemInfoMapper.selectCount(queryWrapperSystemInfo);
			return ResponseMessage.success(list,selectCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseMessage.fail();
    }
	
	/**
	 * @Param{ SystemInfo entity }
	 * 添加系统
	 */
	@PostMapping("addSystemInfo")
    public ResponseMessage addSystemInfo(@RequestBody SystemInfo system) 
	{
		try {
			system.setId(UUID.randomUUID().toString());
			system.setCreatedTime(LocalDateTime.now());
			int insertRows = systemInfoMapper.insert(system);
			return ResponseMessage.success(insertRows,"插入成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }
	
	/**
	 * @Param{ SystemInfo entity }
	 * 修改系统信息
	 */
	@PostMapping("updateSystemInfo")
    public ResponseMessage updateSystemInfo(@RequestBody SystemInfo system) 
	{
		try {
			int updateRows = systemInfoMapper.updateById(system);
			return ResponseMessage.success(updateRows,"修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMessage.fail();
		}
    }

}

